<template>
    <Modal
            width="800"
            v-model="visible"
            :title="title"
            :key="key"
    >
        <div style="width: 80%;padding-left: 10%;padding-right: 10%">
            <Spin size="large" fix v-if="Loading"></Spin>
            <Form :label-width="130" :disabled="disableSubmit" ref="formValidate" :model="formValidate"
                  :rules="ruleValidate">
                <Form-item label="名称:" prop="sceneName">
                    <Input v-model="formValidate.sceneName" placeholder="请输入名称"/>
                </Form-item>
                <Form-item label="描述:" prop="sceneDesc">
                    <Input v-model="formValidate.sceneDesc" placeholder="请输入描述"/>
                </Form-item>
                <Form-item label="标识" prop="sceneIdentify">
                    <Input v-model="formValidate.sceneIdentify" placeholder="请输入标识"/>
                </Form-item>
                <Form-item label="实体:" prop="ruleEntityId">
                    <Select v-model="formValidate.ruleEntityId" placeholder="请选择实体" :disabled="ruleEntityDisAble">
                        <Option v-for="item in ruleEntityList" :value="item.id" :key="item.id">{{item.entityName}}
                        </Option>
                    </Select>
                </Form-item>
                <Form-item label="备注">
                    <Input v-model="formValidate.remarks" placeholder="请输入备注"/>
                </Form-item>
            </Form>
        </div>
        <div slot="footer">
            <Button @click="cancel">取消</Button>
            <template v-if="!disableSubmit">
                <Button type="primary" @click="ok">确定</Button>
            </template>
        </div>
    </Modal>
</template>

<script>
    import {getAction, postAction} from '@/api/request'

    export default {
        data() {
            return {
                modal1: false,
                title: "",
                key: "",
                visible: false,
                disableSubmit: false,
                Loading: false,
                formValidate: {},
                ruleEntityList: {},
                ruleEntityItem: {},
                ruleEntityDisAble: false,
                ruleValidate: {
                    sceneName: [
                        {required: true, message: '动作名称不能为空', trigger: 'blur'}
                    ],
                    sceneDesc: [
                        {required: true, message: '动作描述不能为空', trigger: 'blur'}
                    ],
                    actionClass: [
                        {required: true, message: '实体名称不能为空', trigger: 'blur'}
                    ],
                    sceneIdentify: [
                        {required: true, message: '标识不能为空', trigger: 'blur'}
                    ],
                    ruleEntityId: [
                        {required: true, message: '请选择实体', trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            ok() {
                var that = this;
                this.$refs['formValidate'].validate((valid) => {
                    if (valid) {
                        that.submitFrom();
                    }
                })
            },

            submitFrom() {
                var that = this;
                that.Loading = true;
                var url = "/api/ac/subsistence/RuleSceneInforService/insert";
                if (this.formValidate.id) {
                    url = "/api/ac/subsistence/RuleSceneInforService/update"
                }
                postAction(url, this.formValidate).then((res) => {
                    if (res.data.errcode != 0) {
                        this.$Message.error(res.data.errmsg);
                    } else {
                        this.$emit('ok');
                        this.visible = false;
                    }

                }).finally(() => {
                    that.Loading = false;
                });
            },
            cancel() {
                this.visible = false;
                this.$emit('close')
            },
            add() {
                this.edit({});
                this.ruleEntityDisAble = false;
            },
            edit(row) {
                this.visible = true;
                this.ruleEntityDisAble = true;
                this.formValidate = Object.assign({}, row);
                this.getRuleEntityList();

            },
            //获取实体列表
            getRuleEntityList() {
                var that = this;
                getAction("/api/ac/subsistence/RuleEntityService/list", {}).then((res) => {
                    that.ruleEntityList = res.data.data;
                })
            },
        }
    }
</script>

<style scoped>

</style>